package com.wcs.exam.common.base;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serial;
import java.io.Serializable;

/**
 * 接口返回对象实体
 *
 * @author wcs
 */
@Data
public final class Result<T> implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(Result.class);

    public static final Integer SUCCESS = 200;

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private Integer code = 9999;

    /**
     * 错误信息
     */
    private String msg = "系统错误";

    /**
     * 返回结果实体
     */
    private T data;

    private Result() {
    }

    private Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> error(String msg) {
        logger.error("返回错误：msg={}", msg);
        return new Result<>(9999, msg);
    }

    public static <T> Result<T> error(int code, String msg) {
        logger.error("返回错误：code={}, msg={}", code, msg);
        return new Result<>(code, msg);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS, "", data);
    }
}
